package com.yunpeng.aidevice.common.controllers.configs;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yunpeng.aidevice.biz.service.INoticeBizService;
import com.yunpeng.aidevice.common.constant.ConfigConstantValues;
import com.yunpeng.common.dto.Result;
import com.yunpeng.aidevice.common.dto.cache.LoginUserCacheDTO;
import com.yunpeng.aidevice.common.dto.save.DeviceNoticeParameterSaveDTO;
import com.yunpeng.aidevice.common.dto.save.NoticeUserSaveDTO;
import com.yunpeng.common.util.ParametersCheckUtil;
import com.yunpeng.aidevice.common.vo.device.DeviceNoticeUserConfigVO;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * NoticeController
 *
 * @author : Moore(hemw)
 * @date : 2020-04-28
 */
@RestController
@RequestMapping("/api/notice")
public class NoticeController {

    @Resource
    private INoticeBizService noticeBizService;

    /**
     * 规则下拉菜单
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "ruleDropdownList", method = RequestMethod.POST)
    public Result<JSONArray> ruleDropdownList() {
        return noticeBizService.ruleDropdownList();
    }


    /**
     * 规则参数下拉列表
     *
     * @param requestParam
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "ruleParameterDropdownList", method = RequestMethod.POST)
    public Result<JSONArray> ruleParameterDropdownList(@RequestBody JSONObject requestParam) {
        ParametersCheckUtil.mapIsNotNullOrderEmpty(requestParam, "没有传递任何参数");
        JSONArray scriptCodes = requestParam.getJSONArray("scriptCodes");
        String deviceCode = requestParam.getString("deviceCode");
        ParametersCheckUtil.isNotBlank(deviceCode, "设备代码为空");
        ParametersCheckUtil.collectionIsNotNullOrderEmpty(scriptCodes, "规则代码为空");
        List<String> scriptCodeList = scriptCodes.toJavaList(String.class);
        return noticeBizService.ruleParameterDropdownList(scriptCodeList, deviceCode);
    }

    /**
     * 删除提醒用户
     *
     * @param requestParam
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "removeNoticeToUser", method = RequestMethod.POST)
    public Result<Void> removeNoticeToUser(@RequestBody JSONObject requestParam) {
        ParametersCheckUtil.mapIsNotNullOrderEmpty(requestParam, "没有传递任何参数");
        String deviceCode = requestParam.getString("deviceCode");
        String scriptCode = requestParam.getString("scriptCode");
        String userCode = requestParam.getString("userCode");
        ParametersCheckUtil.isNotBlank(deviceCode, "设备代码为空");
        ParametersCheckUtil.isNotBlank(scriptCode, "脚本代码为空");
        ParametersCheckUtil.isNotBlank(userCode, "用户代码为空");
        return noticeBizService.removeNoticeToUser(scriptCode, userCode, deviceCode);
    }

    /**
     * 设备提醒用户列表
     *
     * @param requestParam
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "deviceNoticeUserList", method = RequestMethod.POST)
    public Result<List<DeviceNoticeUserConfigVO>> deviceNoticeUserList(@RequestBody JSONObject requestParam) {
        ParametersCheckUtil.mapIsNotNullOrderEmpty(requestParam, "没有传递任何参数");
        String deviceCode = requestParam.getString("deviceCode");
        ParametersCheckUtil.isNotBlank(deviceCode, "设备代码为空");
        return noticeBizService.deviceNoticeUserList(deviceCode);
    }

    /**
     * 新增设备提醒用户
     *
     * @param noticeUserSaveDTO
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "addNoticeToUser", method = RequestMethod.POST)
    public Result<Void> addNoticeToUser(@RequestBody NoticeUserSaveDTO noticeUserSaveDTO) {
        ParametersCheckUtil.isNotNull(noticeUserSaveDTO, "没有传递任何参数");
        ParametersCheckUtil.isNotNull(noticeUserSaveDTO.getDeviceCode(), "设备代码为空");
        ParametersCheckUtil.isNotNull(noticeUserSaveDTO.getUserCode(), "用户代码为空");
        ParametersCheckUtil.collectionIsNotNullOrderEmpty(noticeUserSaveDTO.getScripts(), "脚本代码为空");
        LoginUserCacheDTO loginUserCacheDTO = ConfigConstantValues.getLoginUserCacheDTO();
        noticeUserSaveDTO.setTenantCode(loginUserCacheDTO.getTenantCode());
        noticeUserSaveDTO.setMerchantCode(loginUserCacheDTO.getMerchantCode());
        return noticeBizService.addNoticeToUser(noticeUserSaveDTO, loginUserCacheDTO.getCode());
    }

    /**
     * 配置设备提醒参数
     *
     * @param deviceNoticeParameterSaveDTO
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "configDeviceNoticeParameters", method = RequestMethod.POST)
    public Result<Void> configDeviceNoticeParameters(@RequestBody DeviceNoticeParameterSaveDTO deviceNoticeParameterSaveDTO) {
        ParametersCheckUtil.isNotNull(deviceNoticeParameterSaveDTO, "没有传递任何参数");
        ParametersCheckUtil.isNotNull(deviceNoticeParameterSaveDTO.getDeviceCode(), "设备代码为空");
        ParametersCheckUtil.collectionIsNotNullOrderEmpty(deviceNoticeParameterSaveDTO.getScripts(), "脚本代码为空");
        LoginUserCacheDTO loginUserCacheDTO = ConfigConstantValues.getLoginUserCacheDTO();
        deviceNoticeParameterSaveDTO.setTenantCode(loginUserCacheDTO.getTenantCode());
        deviceNoticeParameterSaveDTO.setMerchantCode(loginUserCacheDTO.getMerchantCode());
        return noticeBizService.configDeviceNoticeParameters(deviceNoticeParameterSaveDTO, loginUserCacheDTO.getCode());
    }

}
